package com.handinfo.uimenu;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;

/**
 * 内容窗 1:AlertBox 2:MessageBox 4:DialogMenu 5:popMenu
 * 
 * @author venizeng
 * 
 */
public abstract class UIAbstractContext extends UIAbstractWindow
{
	/**
	 * 最小内容层显示宽度,屏幕的1/4
	 */
	protected static final int W_MIN_CONTENT = 1;
	/**
	 * 最大内容层显示宽度,屏幕的3/4
	 */
	protected static final int W_MAX_CONTENT = (AbstractGameScreen.SCREEN_WIDTH * 3/4);
	/**
	 * 传入数据类型
	 */
	protected static final int TYPE_STR_NOR = 0;//普通字符串
	protected static final int TYPE_STR_NORS = 1;//字符串数组
	protected static final int TYPE_STR_HYPER = 2;//超链接字符串
	//
	protected final int INITX_CONTENT = 8;//内容层的起始绘制x
	protected final int INITY_CONTENT = 8;//内容层的起始绘制x
	//锚点
	public static final int ANCHOR_LT = 1;
	public static final int ANCHOR_RT = 2;
	public static final int ANCHOR_HT = 3;
	/**
	 * 传入内容的类型
	 */
	protected byte m_type;
	/**
	 * 传入内容
	 */
	protected Object m_data;

	/**
	 * 内容显示宽度
	 * alertbox 显示消息
	 * popmenu items
	 * messagebox 显示消息
	 * dialogmenu items
	 */
	protected int m_w_content;

	/**
	 * 内容显示高度
	 * alertbox 显示消息
	 * popmenu items
	 * messagebox 显示消息
	 * dialogmenu items
	 */
	protected int m_h_content;
	
	protected UIAbstractContext(int id)
	{
		super(id);
	}

	/**
	 * 屏蔽父类方法
	 */
	protected void setBounds(int x, int y, int w, int h)
	{
	}
	
	/**
	 * 计算内容层数据高度
	 * @param type
	 * @param data
	 * @return
	 */
	protected int getDataHeight(byte type, Object data)
	{
		if (data == null) {
			return 0 ;
		}
		int tempH = 0;//绘制的高度
		switch (type) {
			case TYPE_STR_NOR: {
				tempH = UITools.H_PRE;
			}
				break;
			case TYPE_STR_NORS: {
				String[] strs = (String[]) data;
				if (strs == null) {
					return 0 ;
				}
				for (int i = 0; i < strs.length; i++) {
					if (strs[i] == null) {
						continue ;
					}
					tempH += UITools.H_PRE ;
				}
			}
				break;
			case TYPE_STR_HYPER: {
				AbsHyperLinker hyperLinker = (AbsHyperLinker) data;
				tempH = hyperLinker.getRealHeight();
			}
				break;
			default:
				throw new IllegalArgumentException("无此类型  type = " + m_type);
		}
		return tempH;
	}

	/**
	 * 绘制内容层数据
	 * @param g
	 * @param x
	 * @param y
	 */
	protected final void paintContentLayer(Graphics g, int tempX, int tempY)
	{
		 paintContentLayer(g, tempX, tempY, m_type, m_data);
	}

	protected final void paintContentLayer(Graphics g, int initX, int initY, byte type, Object data)
	{
		 paintContentLayer(g, initX, initY, UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS), type, data);
	}

	protected final void paintContentLayer(Graphics g, int initX, int initY, int color, byte type, Object data)
	{
		if (data == null) {
			return ;
		}
		switch (type) {
			case TYPE_STR_NOR: {
				String str = (String) data;
				g.setColor(color);
				g.drawString(str, initX, initY, 0);
			}
				break;
			case TYPE_STR_NORS: {
				String[] strs = (String[]) data;
				g.setColor(color);
				int tempY = initY;
				for (int i = 0; i < strs.length; i++) {
					if (strs[i] == null) {
						continue;
					}
					g.drawString(strs[i], initX, tempY, 0);
					tempY += AbstractGameScreen.FONT_H;
				}
			}
				break;
			case TYPE_STR_HYPER: {
				AbsHyperLinker hyperLinker = (AbsHyperLinker) data;
				hyperLinker.paintHyperLink(g, initX, initY);
			}
				break;
			default:
				throw new IllegalArgumentException("无此类型  type = " + m_type);
		}
	}

	/**
	 * 绘制带边框的盒子
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	protected final void paintBox(Graphics g, int x, int y, int w, int h)
	{
//		g.setColor(0x590c00);
//		g.drawRect(x, y, w, h);
//		g.setColor(0xffffdb);
//		g.drawRect(x + 1, y + 1, w - 2, h - 2);
//		g.setColor(0xcfe3c7);
//		g.fillRect(x + 2, y + 2, w - 3, h - 3);
//		g.setColor(COLOR_STR);
//		g.drawRect(x + 2, y + 2, w - 4, h - 4);
		//左顶点
		g.drawRect(x + 2, y + 2, 2, 4);
		g.drawRect(x + 2, y + 2, 4, 2);
		//右顶点
		g.drawRect(x + w - 6, y + 2, 4, 2);
		g.drawRect(x + w - 4, y + 2, 2, 4);
		//左下顶点
		g.drawRect(x + 2, y + h - 6, 2, 4);
		g.drawRect(x + 2, y + h - 4, 4, 2);
		//右下顶点
		g.drawRect(x + w - 6, y + h - 4, 4, 2);
		g.drawRect(x + w - 4, y + h - 6, 2, 4);
	}

	/**
	 * 绘制选中框
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	protected void paintSelItem(Graphics g, int x, int y, int w, int h)
	{
		UITools.drawRect(g, x, y, w, h, 0x946300, 0) ;
		UITools.drawRect(g, x, y, w, h, 0xffd447, 1) ;
		UITools.fillRect(g, x, y, w, h, 0x8c5a5a, 2) ;
	}

	protected void keyLogic()
	{

	}

}
